const http = require('./api.js');
import {
    request
} from '../libs/wx-promise-request';

function toData(inputTime, type) {
    //type 表示从哪个页面取调用的
    var nowdata = new Date(); //当前时间
    var ny = nowdata.getFullYear(); //当前年
    var nm = nowdata.getFullYear(); //当前月
    var nt = nowdata.getFullYear(); //当前天

    var date = new Date(inputTime);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    m = m < 10 ? ('0' + m) : m;
    d = d < 10 ? ('0' + d) : d;

    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    minute = minute < 10 ? ('0' + minute) : minute;
    if (type) { //从详情页面过来时间显示完全调用  toData(12344389343898,'1')
        return y + '年' + m + '月' + d + ' ' + h + ':' + minute;
    } else {
        if (ny == y) { //证明是本年只显示月份
            return m + '月' + d + ' ' + h + ':' + minute;
        } else if (ny == y && nm == m && d == nd) { //证明是本年的本月本天
            return h + ':' + minute;
        } else {
            return y + '年' + m + '月' + d + ' ' + h + ':' + minute;
        }
    }


}

function login() {

    return new Promise((resolve, reject) => {
        wx.login({
            success: res => {
                let code = res.code
                //获取用户信息
                wx.getUserInfo({
                    withCredentials: true,
                    success: res => {
                        wx.setStorageSync('sign', 'yes')
                        let url = http.api.login;
                        myHttps({
                            url: url,
                            method: 'POST',
                            data: {
                                'code': code,
                                'encryptedData': res.encryptedData,
                                'iv': res.iv,
                            }
                        }).then(res => {
                            wx.setStorageSync(
                                'no_login', 'no'
                            )
                            wx.setStorageSync('userId', res.data.content.id)
                            wx.setStorageSync('userInfo', res.data.content)
                            let token = res.data.token;
                            wx.setStorageSync(
                                'Authorization', 'Bearer ' + token
                            )
                            resolve(res)
                        })
                    },
                    fail: function(res) {
                        wx.setStorageSync(
                            'no_login', 'yes'
                        )
                    }
                })

            },
            fail: function(res) {

            }
        })
    })
}

const souHttps = object => request({
    ...object,

    header: {
        ...object.header,
        // 'Accept': 'text/html, application/xhtml+xml,application/xml; q=0.9,image/webp,image/apng, */*;q=0.8',
        // 'Accept-Encoding': 'gzip, deflate, br',
        // 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
        // 'Cache-Control': 'max-age=0',
        // 'Connection': 'keep-alive',
        // 'Host': 'weixin.sogou.com',
        // 'If-Modified-Since': 'Fri, 16 Nov 2018 09:39:41 GMT',
        // 'If-None-Match': '146533bd569179868cf3b0d466a54395',
        // 'Upgrade-Insecure-Requests': 1,
        // 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X)AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1',
        // 'Referer': wx.getStorageSync('urls'),
        // 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
    }
}).then(function(res) {
    if (res.statusCode == 200) {
        return res.data
    } else if (res.statusCode == 401 || res.statusCode == 403 || res.code == 401 || res.code == 403) {
        login()
        wx.showModal({
            title: '抱歉',
            content: '网路不给力,请重试',
            confirmText: '点击重试',
            showCancel: false,
            success: function(res) {
                if (res.confirm) {
                    login().then((res) => {
                        wx.switchTab({
                            url: '/pages/index/index',
                        })
                    })
                }
            }
        })
    } else {
        // console.log('请求失败')
        wx.showLoading({
            title: '服务器开小差...',
        })
        setTimeout(() => {
            wx.hideLoading()
        }, 500)
    }
})
const myHttpHeader = object => request({
    ...object,
    header: {
        ...object.header,
        // 'Accept': 'text/html, application/xhtml+xml,application/xml; q=0.9,image/webp,image/apng, */*;q=0.8',
        // 'Accept-Encoding': 'gzip, deflate, br',
        // 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
        // 'Cache-Control': 'max-age=0',
        // 'Connection': 'keep-alive',
        // 'Host': 'weixin.sogou.com',
        // 'If-Modified-Since': 'Fri, 16 Nov 2018 09:39:41 GMT',
        // 'If-None-Match': '146533bd569179868cf3b0d466a54395',
        // 'Upgrade-Insecure-Requests': 1,
        // 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X)AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1',
        // 'Referer': wx.getStorageSync('urls'),
        // 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
    }
})
module.exports = {
    login: login,
    souHttps: souHttps,
    toData: toData
}